﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SuperStarProject.Models;
using System.Data.SqlClient;
using System.Data;

namespace SuperStarProject.DAO
{
    public class QuaTangDAO
    {
        public static bool CreateQuaTang(QuaTangModel model, string manhanvien)
        {
            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@manv", (object)manhanvien));
            lstParameter.Add(new SqlParameter("@maqua", (object)model.MaQua));
            lstParameter.Add(new SqlParameter("@masp", (object)model.MaSanPham));
            lstParameter.Add(new SqlParameter("@ten", (object)model.Ten));
            lstParameter.Add(new SqlParameter("@diemthuongcan", (object)model.DiemThuong));
            lstParameter.Add(new SqlParameter("@max", (object)model.SoLuongToiDaDuocChon));
            lstParameter.Add(new SqlParameter("@nhap", (object)model.SoLuongNhapKho));
            lstParameter.Add(new SqlParameter("@hinh", (object)model.HinhAnh));

            int result = DataProvider.RunStoredProcParams_NoRead("sp_themqua", db, lstParameter);
            if (result == 0)
                return true;
            return false;
        }

        public static List<QuaTangModel> ShowAll(string id)
        {
            QuaTangModel prod = new QuaTangModel();
            SqlParameter para_productId = new SqlParameter("@maSP", (object)id);

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(para_productId);

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_QuaTang_ByMaSP", db, lstParameter);

            return Convert(rdr);
        }
        public static QuaTangModel ViewDetails(string id)
        {
            QuaTangModel prod = new QuaTangModel();
            SqlParameter para_productId = new SqlParameter("@ma", (object)id);

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(para_productId);

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_QuaTang_dk", db, lstParameter);
            if (rdr.HasRows)
                return Convert(rdr).First();
            else
                throw new OperationCanceledException();
        }
        public static int GetSoLuongToiDaCoThe(string quaId)
        {
            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@maqua", (object)quaId));
            SqlParameter soluongcon = new SqlParameter("@soluongcon", SqlDbType.Int);
            soluongcon.Direction = ParameterDirection.Output;
            lstParameter.Add(soluongcon);
            try
            {
                return int.Parse(DataProvider.RunStoredProcParams_ReadOutPut("sp_soluongqua_left", db, lstParameter, "@soluongcon"));
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        static List<QuaTangModel> Convert(SqlDataReader rdr)
        {
            List<QuaTangModel> lstItem = new List<QuaTangModel>();
            while (rdr.Read())
            {
                QuaTangModel item = new QuaTangModel();
                item.DiemThuong = (int)rdr["DiemThuongCan"];
                item.HinhAnh = (string)rdr["HinhAnh"];
                item.MaQua = (string)rdr["MaQua"];
                item.MaSanPham = (string)rdr["MaSanPham"];
                item.SoLuongNhapKho = GetSoLuongToiDaCoThe(item.MaQua);
                item.SoLuongToiDaDuocChon = (int)rdr["SoLuongToiDaDuocChon"];
                item.Ten = (string)rdr["Ten"];
                lstItem.Add(item);
            }
            return lstItem;
        }

    }
}
